/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoUnit.*;

/**
 * <p>Project: javaseapp - T2
 * <p>Powered by webrx On 2021-08-31 11:31:26
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T2 {
    @Test
    public void d9() {
        //Date相关的类 相互转换
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        var d1 = new Date(System.currentTimeMillis());
        try {
            var d2 = sdf.parse("1985-10-20 10:20:30");

        } catch (Exception e) {
            e.printStackTrace();
        }

        //Date 转 新的类
        var dd = new Date();
        var dd1 = dd.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        var dd2 = dd.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDate();
        var tt3 = dd.toInstant().atOffset(ZoneOffset.of("+8")).toLocalTime();
        System.out.println(dd);
        System.out.println(dd1);
        System.out.println(dd2);
        System.out.println(tt3);

        //LocalDateTime 转 Date
        var nd1 = LocalDateTime.now();
        var nd2 = Date.from(nd1.toInstant(ZoneOffset.of("+8")));
        System.out.println(nd2);

        var nd3 = new Date(nd1.toInstant(ZoneOffset.of("+8")).toEpochMilli());
        System.out.println(nd3);

        var nd4 = Date.from(Instant.now());
        System.out.println(nd4);

    }

    @Test
    public void d8() {
        //格式化
        var t1 = LocalTime.now();
        System.out.println(t1);

        //美国时间标准
        var tt = DateTimeFormatter.ofPattern("HH:mm:ss a", Locale.US);
        System.out.println(t1.format(tt));

        var tt2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 E");
        System.out.println(LocalDate.now().format(tt2));

        //格式化日期时间
        var t = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss E a");
        System.out.println(LocalDateTime.now().format(t));


    }

    @Test
    public void d7() {
        //生成指定范围的随机日期
        var d1 = LocalDate.of(2010, 1, 1);//开始日期
        var d2 = LocalDate.now();//结束日期为当前日期
        var rr = new Random();
        var yy = rr.nextInt(d2.getYear() - d1.getYear() + 1) + d1.getYear();
        var mm = rr.nextInt(12) + 1;
        var d3 = LocalDate.of(yy, mm, 1); //通过此对象可以获取最大多天，然后危机天数就是日期
        var d4 = d3.plus(rr.nextInt(d3.lengthOfMonth()), DAYS);
        System.out.println(d4);
        var d5 = LocalDate.of(yy, mm, rr.nextInt(d3.lengthOfMonth()) + 1);
        System.out.println(d5);
    }

    @Test
    public void d6() {
        var d1 = LocalDate.of(1988, 7, 22);
        var d2 = LocalDate.now();
        System.out.printf("%s 至 %s 共有 %d 天%n", d1, d2, DAYS.between(d1, d2));
        System.out.printf("%s 至 %s 共有 %d 月%n", d1, d2, MONTHS.between(d1, d2));
        System.out.printf("%s 至 %s 共有 %d 年%n", d1, d2, YEARS.between(d1, d2));

        var d3 = LocalDate.now().getDayOfYear();
        System.out.println(d3); //这一年的243天
        var d4 = LocalDate.now().getDayOfMonth();
        System.out.println(d4);//这一月31

        //显示当前日期星期几
        var d5 = LocalDate.now().getDayOfWeek();
        System.out.println(d5);//TUESDAY
        DayOfWeek dw = LocalDate.now().getDayOfWeek();
        //Returns: //the day-of-week, from 1 (Monday) to 7 (Sunday)

        System.out.println(dw.getValue());
        System.out.printf("%s 是：星期%s %n", LocalDate.now(), "一二三四五六日".charAt(dw.getValue() - 1));
    }

    @Test
    public void d5() {
        //求当前月最后一天的日期
        var d1 = LocalDate.now();
        var d2 = LocalDate.of(d1.getYear(), d1.getMonthValue(), d1.lengthOfMonth());
        System.out.println(d2);

        //求下月最后一天日期
        var d3 = LocalDate.of(d1.getYear(), d1.getMonthValue() + 1, 1);
        var d4 = LocalDate.of(d3.getYear(), d3.getMonthValue(), d3.lengthOfMonth());
        System.out.println(d4);
        //当前月最后一天 日期 2021-08-31
        var two = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(two);
        var two2 = LocalDate.now().with(t -> t.with(DAY_OF_MONTH, t.range(DAY_OF_MONTH).getMaximum()));
        System.out.println(two2);

        //获取下个月最后一天日期 2021-09-30
        var twonext = LocalDate.now().plus(1, MONTHS).with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(twonext);
        //获取上个月最后一天日期  2021-07-31
        var twoprev = LocalDate.now().minus(1, MONTHS).with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(twoprev);

    }

    @Test
    public void d4() {
        //求当前月1号日期
        var d1 = LocalDate.now().with(t -> t.with(DAY_OF_MONTH, 1));
        System.out.println(d1);
        //求下一个月1号日期
        var d2 = LocalDate.now().with(t -> t.with(DAY_OF_MONTH, 1).plus(1, MONTHS));
        System.out.println(d2);
        //求上个月1号日期
        var d3 = LocalDate.now().with(t -> t.with(DAY_OF_MONTH, 1).minus(1, MONTHS));
        System.out.println(d3);

        //求指定日期的1号日期
        var d4 = LocalDate.of(1985, 3, 16).with(t -> t.with(DAY_OF_MONTH, 1));
        System.out.println(d4);
    }

    @Test
    public void d3() {
        //求指定日期的月最多有多少天
        var yy = 2021;
        var mm = 2;
        System.out.println(LocalDate.of(yy, mm, 1).lengthOfMonth());
        System.out.println();
        var y = 1900;
        var i = 0;
        for (; y <= Year.now().getValue(); y++) {
            if (Year.of(y).isLeap()) {
                System.out.printf("%d ", y);
                if (++i % 5 == 0) {
                    System.out.println();
                    i = 0;
                }
            }
        }

        for (int m = 1; m <= 12; m++) {
            int y1 = LocalDate.now().getYear();
            int d1 = LocalDate.of(y, m, 1).lengthOfMonth();
            System.out.printf("%d年%d月(%d)天 %n", y1, m, d1);
        }
    }

    @Test
    public void d2() {
        //判断当前年是不是闰年  java.time.LocalDate类
        var d1 = LocalDate.now();
        String info = String.format("%s年是%s%n", d1.getYear(), d1.isLeapYear() ? "闰年" : "平年");
        System.out.println(info);

        //判断指定年份  java.time.Year 类
        var d2 = Year.of(2020);
        String d2info = String.format("%s年是%s%n", d2.getValue(), d2.isLeap() ? "闰年" : "平年");
        System.out.println(d2info);

        //使用java.time.Month类，可以求一个月天数
        var m = Month.of(2);
        System.out.println(m.maxLength());//29
        System.out.println(m.minLength());//28

        //求出指定日期最多有多少天  LocalDate .lengthOfMonth(); 返回的是当前月的天数 int
        var d3 = LocalDate.of(2020, 2, 1).lengthOfMonth();
        System.out.println(d3);//29 闰年 29

        //2021 8 31 这个月最多有31天
        System.out.println(LocalDate.now().lengthOfMonth());//31

        //当前月1日 日期 2021-08-01
        var one = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(one);
        //下月1日 日期 2021-09-01
        var onenext = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(onenext);
        //上月1日 日期 2021-07-01
        var oneprev = LocalDate.now().minus(1, ChronoUnit.MONTHS).with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(oneprev);
        //上月1日 日期 2021-07-01
        var oneprev2 = LocalDate.now().with((e) -> e.with(DAY_OF_MONTH, 1).plus(-1, MONTHS));
        System.out.println(oneprev2);

        //当前月最后一天 日期 2021-08-31
        var two = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(two);
        var two2 = LocalDate.now().with(t -> t.with(DAY_OF_MONTH, t.range(DAY_OF_MONTH).getMaximum()));
        System.out.println(two2);

        //获取下个月最后一天日期 2021-09-30
        var twonext = LocalDate.now().plus(1, MONTHS).with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(twonext);
        //获取上个月最后一天日期  2021-07-31
        var twoprev = LocalDate.now().minus(1, MONTHS).with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(twoprev);

        //求出两个日期之间的天数
        var da = LocalDate.of(1985, 10, 25);
        var db = LocalDate.now();
        Duration days = Duration.between(da.atStartOfDay(), db.atStartOfDay());
        System.out.println(days.toDays());//13094 天

        long ddd = ChronoUnit.DAYS.between(da, db);
        System.out.println(ddd);//13094天

        System.out.println(MONTHS.between(da, db));//430个月
        System.out.println(ChronoUnit.YEARS.between(da, db));//35年

        //生成随机日期(指定范围）
        //2000-01-01  2021-08-31
        var dd1 = LocalDate.of(2000, 1, 1);
        var dd2 = LocalDate.of(2021, 8, 31);
        Random rand = new Random();
        var yy = rand.nextInt(dd2.getYear() - dd1.getYear() + 1) + dd1.getYear();
        var mm = rand.nextInt(11) + 1;
        var dd = rand.nextInt(LocalDate.of(yy, mm, 1).lengthOfMonth() + 1) + 1;
        var drand = LocalDate.of(yy, mm, dd);
        System.out.println(drand);

    }

    @Test
    public void d1() {
        //输出年月日
        var d1 = new Date();
        System.out.printf("%1$tF %1$tT%n", d1);

        //java 1.8 LocalDate
        var d2 = LocalDate.now(); //只有年月日
        System.out.printf("%1$tF%n", d2);
        System.out.println(d2);

        //java 1.8 LocalDateTime
        var d3 = LocalDateTime.now();
        System.out.printf("%1$tF %1$tT%n", d3);
        System.out.println(d3);

        var d4 = LocalTime.now();
        System.out.printf("%1$tT %n", d4);
        System.out.println(d4);

        System.out.println("------------------------------");
        System.out.printf("%1$tF %1$tT%n", System.currentTimeMillis());


        var d5 = Calendar.getInstance();
        System.out.printf("%1$tF %1$tT%n", d5);

    }
}
